﻿ 
#region

using System.Linq;
using System.Windows;
using Microsoft.Practices.Composite.Regions;
using pushbomb.composite.core;

#endregion

namespace HitTestItemSelector
{
    /// <summary>
    /// 
    /// </summary>
    public class SelectorViewModel : ViewModelBase 
    {
         private IRegion _regionMain;
        private readonly ItemCountSelector _view;

        /// <summary>
        /// Initializes a new instance of the <see cref="SelectorViewModel"/> class.
        /// </summary>
        /// <param name="fs">The fs.</param>
        /// <param name="view">The view.</param>
        public SelectorViewModel(IFrameworkServiceProvider fs, IView view)
            : base(fs, view)
        {
            _view  = view as ItemCountSelector;
            if (_view != null) _view.Loaded  += OnLoaded;
        }


        private void OnLoaded(object sender1, RoutedEventArgs e)
        {
            _regionMain = FrameworkServices.RegionManager.Regions["MainRegion"];
            _regionMain.Views.CollectionChanged +=
                (sender, args) => _view.txtCount.Text = ((IViewsCollection) sender).Count().ToString();
            _view.txtCount.Text = _view.slider.Value.ToString();
            _view.slider.Value = _regionMain.Views.Count();
        }
    }
}